import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.activeX.ActiveXComponent;

/**
 * 文本转语音
 */
public class TestToSpeech {
    String str = new String();
    // 转入字符串
    public TestToSpeech(String str){
        this.str = str;
    }
    // 读
    public void read() {
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
        Dispatch sapo = sap.getObject();
        try {
            // 音量 0-100
            sap.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            sap.setProperty("Rate", new Variant(0));
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant(this.str));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sapo.safeRelease();
            sap.safeRelease();
        }
    }
}
